package com.xxx.control.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.POWER_SERVICE
import android.content.Intent
import android.os.PowerManager
import com.xxx.control.activity.HomeActivity

class AlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action == AlarmUtils.ALARM_ACTION) {
            val wakeLock = (context.getSystemService(POWER_SERVICE) as PowerManager)
                .newWakeLock(
                    PowerManager.PARTIAL_WAKE_LOCK,
                    "MyApp::AlarmWakeLock"
                ).apply {
                    acquire(60_000)
                }

            try {
                // 执行每日清理任务
                performDailyClear(context)
            } finally {
                wakeLock.release()
            }
        }
    }
    private fun performDailyClear(context: Context) {
        // 实现具体清理逻辑
        val prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
        // 日期不一致，清空当今日进馆数
        prefs.edit().putInt("inCount", 0).apply()

        val activityIntent = Intent(context, HomeActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        context.startActivity(activityIntent)
    }
}